package com.aiq.controller;

import com.aiq.constants.ConstantCode;
import com.aiq.exceptions.MyBaseException;
import com.aiq.vo.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName: MyExceptionHandler
 * @Description: 自定义异常处理程序
 * @Author: vie0134
 * @Date: 2021-01-11 12:44
 * @Version: 1.0
 **/
@ControllerAdvice
public class MyExceptionHandler {

    /**
     * @Author: vie0134
     * @Description: 处理自定义异常,通过返回错误数据,告诉前端,错误信息
     * @Date: 12:45 2021/1/11
     * @Param: e  自定义异常对象
     * @return: R 统一数据的返回对象
     **/
    @ResponseBody
    @ExceptionHandler(MyBaseException.class)
    public R processMyBaseException(MyBaseException e) {
        return R.setError(null, e.getMessage(), e.getCode());
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public R processBaseException(Exception exception) {
        return R.setError(null, "网络异常，请检查网络", ConstantCode.ERROR);
    }
}
